package com.example.userserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commons.entity.ExpertsDO;
import com.example.commons.entity.TrainingCertificatesDO;
import com.example.commons.entity.UserDO;
import com.example.commons.utils.ApiResult;
import com.example.userserver.config.RedisUtil;
import com.example.userserver.service.ExpertsService;
import com.example.userserver.service.SysBrandService;
import com.example.userserver.service.UserService;
import com.example.userserver.sms.server.TrainingCertificatesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jinZhiIndex/experts")
public class ExpertsController {

    @Autowired
    private ExpertsService expertsService;
    @Autowired
    private TrainingCertificatesService trainingCertificatesService;
    @Autowired
    private UserService userService;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 专家信息接口返回
     * type 1 特聘专家
     *      2 师资
     *      3 高级师资
     *      4 考评员
     *      5 高级考评员
     *      6 督导员
     *      7 考务技术员
     * @return
     */
    @PostMapping("/expertsList")
    public ApiResult<?> expertsList(Integer type ,Integer current, Integer number) {
        if(type==null){
            return ApiResult.fail("参数异常！");
        }

        Map parameterMap =new HashMap();
        parameterMap.put("current",current);
        parameterMap.put("number",number);
        parameterMap.put("type",type);
        String key = redisUtil.createRediskey(parameterMap, "expertsList");
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++缓存key值为："+key);
        Map ca = (Map) redisUtil.get(key);
        if (ca != null){
            System.out.println("专家信息接口已读取到缓存数据++++++++++++++++++++++++++++++++++++++++++++++++++++");
            return ApiResult.success(ca);
        }
        Map map=new HashMap();
        if(type==1){
            Map map1=new HashMap();
            //督导员
            //考务技术员

            QueryWrapper<ExpertsDO> wrapper=new QueryWrapper<>();
            wrapper.like("type", 6);
            wrapper.orderByDesc("num");
            Page<ExpertsDO> producePage = new Page<ExpertsDO>(current,number);
            //特聘专家
            IPage<ExpertsDO> list=   expertsService.page(producePage,wrapper);
            map1.put("list",list.getRecords());
            map1.put("type",1);
            map1.put("listMsg","专家");
            map1.put("listSize",list.getTotal());
            map.put("type",map1);
        }else if(type==3){
            Map map1=new HashMap();
            Page<TrainingCertificatesDO> producePage = new Page<TrainingCertificatesDO>(current,number);
            //高级师资
            QueryWrapper<TrainingCertificatesDO> wrapper1=new QueryWrapper<>();
            wrapper1.like("certificates_type", "高级师资");
          //  wrapper1.orderByDesc("org_name");
            wrapper1.orderByDesc("inch_photograph");
            wrapper1.groupBy("user_id");
          //  wrapper1.orderByDesc("org_name","inch_photograph");
            IPage<TrainingCertificatesDO>  profileDOS =  trainingCertificatesService.page(producePage,wrapper1);
            for(TrainingCertificatesDO li:profileDOS.getRecords()){
                UserDO userDO=  userService.getById(li.getUserId());
                li.setName(userDO.getName()!=null?userDO.getName():"未填写");
            }
            map1.put("list",userOff(profileDOS.getRecords()));
            map1.put("type",3);
            map1.put("listMsg","高级师资");
            map1.put("listSize",profileDOS.getTotal());
            map.put("type",map1);
        }else if(type==2){
            Map map1=new HashMap();
            Page<TrainingCertificatesDO> producePage = new Page<TrainingCertificatesDO>(current,number);

            //师资
            QueryWrapper<TrainingCertificatesDO> wrapper2=new QueryWrapper<>();
            wrapper2.like("numbers", "JZ20");
          //  wrapper2.isNotNull("inch_photograph");
            wrapper2.groupBy("user_id");
         //   wrapper2.orderByDesc("org_name");
            wrapper2.orderByDesc("inch_photograph");
            //wrapper2.orderByDesc("org_name","inch_photograph");
            IPage<TrainingCertificatesDO>     profileDOS =  trainingCertificatesService.page(producePage,wrapper2);
            for(TrainingCertificatesDO li:profileDOS.getRecords()){
                UserDO userDO=  userService.getById(li.getUserId());
                li.setName(userDO.getName()!=null?userDO.getName():"未填写");
            }
            map1.put("type",2);
            map1.put("list",userOff(profileDOS.getRecords()));
            map1.put("listMsg","师资");
            map1.put("listSize",profileDOS.getTotal());
            map.put("type",map1);
        }else if(type==4){
            Page<TrainingCertificatesDO> producePage = new Page<TrainingCertificatesDO>(current,number);
            Map map1=new HashMap();
            //考评员
            QueryWrapper<TrainingCertificatesDO> wrapper3=new QueryWrapper<>();
            wrapper3.like("numbers", "JZ40");
          //  wrapper3.isNotNull("inch_photograph");
          //  wrapper3.orderByDesc("org_name");
            wrapper3.orderByDesc("inch_photograph");
            wrapper3.groupBy("user_id");

            IPage<TrainingCertificatesDO>   profileDOS =  trainingCertificatesService.page(producePage,wrapper3);
            for(TrainingCertificatesDO li:profileDOS.getRecords()){
                UserDO userDO=  userService.getById(li.getUserId());
                li.setName(userDO.getName()!=null?userDO.getName():"未填写");
            }
            map1.put("type",4);
            map1.put("list",userOff(profileDOS.getRecords()));
            map1.put("listMsg","考评员");
            map1.put("listSize",profileDOS.getTotal());
            map.put("type",map1);
        }else if(type==5){
            Page<TrainingCertificatesDO> producePage = new Page<TrainingCertificatesDO>(current,number);
            Map map1=new HashMap();
            //高级考评员
            QueryWrapper<TrainingCertificatesDO> wrapper4=new QueryWrapper<>();
            wrapper4.like("numbers", "JZ30");
          //  wrapper4.orderByDesc("org_name");
            wrapper4.orderByDesc("inch_photograph");
           // wrapper4.isNotNull("inch_photograph");
            wrapper4.groupBy("user_id");
            IPage<TrainingCertificatesDO>  profileDOS =  trainingCertificatesService.page(producePage,wrapper4);
            for(TrainingCertificatesDO li:profileDOS.getRecords()){
                UserDO userDO=  userService.getById(li.getUserId());
                li.setName(userDO.getName()!=null?userDO.getName():"未填写");
            }

            map1.put("list",userOff(profileDOS.getRecords()));
            map1.put("listMsg","高级考评员");
            map1.put("type",5);
            map1.put("listSize",profileDOS.getTotal());
            map.put("type",map1);

        }else if(type==6){
            Page<TrainingCertificatesDO> producePage = new Page<TrainingCertificatesDO>(current,number);
            Map map1=new HashMap();
            //督导员
            QueryWrapper<TrainingCertificatesDO> wrapper5=new QueryWrapper<>();
            wrapper5.like("numbers", "JZ70");

          //  wrapper5.isNotNull("inch_photograph");
            wrapper5.groupBy("user_id");
           // wrapper5.orderByDesc("org_name");
            wrapper5.orderByDesc("inch_photograph");
            IPage<TrainingCertificatesDO>  profileDOS =  trainingCertificatesService.page(producePage,wrapper5);
            for(TrainingCertificatesDO li:profileDOS.getRecords()){
                UserDO userDO=  userService.getById(li.getUserId());
                li.setName(userDO.getName()!=null?userDO.getName():"未填写");
            }
            map1.put("list",userOff(profileDOS.getRecords()));
            map1.put("listSize",profileDOS.getTotal());

            map1.put("listMsg","督导员");
            map1.put("type",6);
            map.put("type",map1);
        }else if(type==7){
            Page<TrainingCertificatesDO> producePage = new Page<TrainingCertificatesDO>(current,number);
            Map map1=new HashMap();
            //考务技术
            QueryWrapper<TrainingCertificatesDO> wrapper6=new QueryWrapper<>();
            wrapper6.like("numbers", "JZ50");

          //  wrapper6.isNotNull("inch_photograph");
            wrapper6.groupBy("user_id");
           // wrapper6.orderByDesc("org_name");
            wrapper6.orderByDesc("inch_photograph");
            IPage<TrainingCertificatesDO>    profileDOS =  trainingCertificatesService.page(producePage,wrapper6);
            for(TrainingCertificatesDO li:profileDOS.getRecords()){
              UserDO userDO=  userService.getById(li.getUserId());
                li.setName(userDO.getName()!=null?userDO.getName():"未填写");
            }
            map1.put("list",userOff(profileDOS.getRecords()));
            map1.put("listSize",profileDOS.getTotal());

            map1.put("listMsg","考务技术员");
            map1.put("type",7);
            map.put("type",map1);
        }
        map.put("expertsNum",expertsNum());
        redisUtil.set(key,map,86400); //一天过期
        return ApiResult.success(map);
    }
    //用户职位信息填充
    public  List<TrainingCertificatesDO> userOff( List<TrainingCertificatesDO> list){

            for(TrainingCertificatesDO li:list){
                //师资
                QueryWrapper<UserDO> wrapper2=new QueryWrapper<>();
                wrapper2.like("numbers", "JZ20");
              //  wrapper2.isNotNull("inch_photograph");
                wrapper2.orderByDesc("org_name");
                UserDO  userDO =  userService.getById(li.getUserId());
                li.setUserOff(StringUtils.isNotBlank(userDO.getOfficialCapacity())==true?userDO.getOfficialCapacity():"幼儿照护师资");
            }


        return list;
    }

    public  Map expertsNum(){
        Map map=new HashMap();
        //考务技术
        QueryWrapper<TrainingCertificatesDO> wrapper6=new QueryWrapper<>();
        wrapper6.like("numbers", "JZ50");
      //  wrapper6.isNotNull("inch_photograph");
        wrapper6.groupBy("user_id");
        List<TrainingCertificatesDO> profileDOS6 =  trainingCertificatesService.list(wrapper6);

        Map map1=new HashMap();
        map1.put("num",profileDOS6.size());
        map1.put("msg","考务技术员");
        map.put("type7",map1);

        //督导员
        QueryWrapper<TrainingCertificatesDO> wrapper5=new QueryWrapper<>();
        wrapper5.like("numbers", "JZ70");
     //   wrapper5.isNotNull("inch_photograph");
        wrapper5.groupBy("user_id");
        List<TrainingCertificatesDO> profileDOS5 =  trainingCertificatesService.list(wrapper5);

        Map map2=new HashMap();
        map2.put("num",profileDOS5.size());
        map2.put("msg","督导员");
        map.put("type6",map2);


        //高级考评员
        QueryWrapper<TrainingCertificatesDO> wrapper4=new QueryWrapper<>();
        wrapper4.like("numbers", "JZ30");
     //   wrapper4.isNotNull("inch_photograph");
        wrapper4.groupBy("user_id");
        List<TrainingCertificatesDO> profileDOS4 =  trainingCertificatesService.list(wrapper4);
       // map.put("type5",profileDOS4.size());

        Map map3=new HashMap();
        map3.put("num",profileDOS4.size());
        map3.put("msg","高级考评员");
        map.put("type5",map3);

        //考评员
        QueryWrapper<TrainingCertificatesDO> wrapper3=new QueryWrapper<>();
        wrapper3.like("numbers", "JZ40");
        wrapper3.groupBy("user_id");
       // wrapper3.isNotNull("inch_photograph");
        List<TrainingCertificatesDO> profileDOS3 =  trainingCertificatesService.list(wrapper3);
      //  map.put("type4",profileDOS3.size());

        Map map4=new HashMap();
        map4.put("num",profileDOS3.size());
        map4.put("msg","考评员");
        map.put("type4",map4);

        //高级师资
        QueryWrapper<TrainingCertificatesDO> wrapper1=new QueryWrapper<>();
        wrapper1.like("certificates_type", "高级师资");
        wrapper1.groupBy("user_id");
        List<TrainingCertificatesDO> profileDOS =  trainingCertificatesService.list(wrapper1);
      //  map.put("type3",profileDOS.size());

        Map map5=new HashMap();
        map5.put("num",profileDOS.size());
        map5.put("msg","高级师资");
        map.put("type3",map5);

        //师资
        QueryWrapper<TrainingCertificatesDO> wrapper2=new QueryWrapper<>();
        wrapper2.like("numbers", "JZ20");
        wrapper2.groupBy("user_id");
     //   wrapper2.isNotNull("inch_photograph");
        List<TrainingCertificatesDO> profileDOS1 =  trainingCertificatesService.list(wrapper2);
       // map.put("type2",profileDOS1.size());

        Map map6=new HashMap();
        map6.put("num",profileDOS1.size());
        map6.put("msg","师资");
        map.put("type2",map6);

        QueryWrapper<ExpertsDO> wrapper=new QueryWrapper<>();
        wrapper.like("type", 6);
        wrapper.orderByDesc("num");
        //特聘专家
        List<ExpertsDO> list=   expertsService.list(wrapper);
      //  map.put("type1",list.size());

        Map map7=new HashMap();
        map7.put("num",list.size());
        map7.put("msg","专家");
        map.put("type1",map7);

        return map;
    }
    /**
     * 专家详情接口返回
     *  type 1特聘专家
     *       2其他信息查看
     * @return
     */
    @PostMapping("/expertsDetails")
    public ApiResult<?> expertsDetails(Integer type,Long id) {
        if(type==null){
            return ApiResult.fail("参数异常！");
        }
        if(type==1){
            Map map=new HashMap();
         ExpertsDO expertsDO=   expertsService.getById(id);
            map.put("date",expertsDO);
            map.put("type",type);
            return ApiResult.success(map);
        }else {
            Map map=new HashMap();

            TrainingCertificatesDO certificatesDO=   trainingCertificatesService.getById(id);

            map.put("type",type);
                if(certificatesDO!=null){
                  UserDO userDO= userService.getById(certificatesDO.getUserId());
                  if(userDO!=null){
                      certificatesDO.setUserOff(userDO.getOfficialCapacity()!=null?userDO.getOfficialCapacity():"幼儿照护师资");
                      certificatesDO.setUserOrgName(userDO.getOrgName()!=null?userDO.getOrgName():"湖南金职伟业母婴护理有限公司");
                      certificatesDO.setName(userDO.getName());
                  }else {
                      return ApiResult.fail("没有更多信息！");
                  }

                    map.put("date",certificatesDO);
                    return ApiResult.success(map);

                }else {
                    return ApiResult.fail("没有更多信息！");
                }

        }

    }


}
